package android.zhcs.myapplication.ui.slideshow;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.zhcs.myapplication.Cac;
import android.zhcs.myapplication.MainActivity2spxq;
import android.zhcs.myapplication.R;
import android.zhcs.myapplication.databinding.FragmentSlideshowBinding;
import android.zhcs.myapplication.databinding.ItemsptpBinding;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

public class SlideshowFragment extends Fragment {

    private SlideshowViewModel slideshowViewModel;
    private FragmentSlideshowBinding binding;
    public ArrayList<sptp> list1 = new ArrayList<sptp>();
    public ArrayList<sptp> list2 = new ArrayList<sptp>();

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        slideshowViewModel =
                new ViewModelProvider(this).get(SlideshowViewModel.class);

        binding = FragmentSlideshowBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        binding.tab.addTab(binding.tab.newTab().setText("违章视频"));
        binding.tab.addTab(binding.tab.newTab().setText("违章图片"));
        list1.add(new sptp(1,R.drawable.spt1,"违章视频01",R.raw.sp1));
        list1.add(new sptp(2,R.drawable.spt2,"违章视频02",R.raw.sp2));
        list1.add(new sptp(3,R.drawable.spt3,"违章视频03",R.raw.sp3));
        list1.add(new sptp(4,R.drawable.spt4,"违章视频04",R.raw.sp4));
        list2.add(new sptp(1,R.drawable.t1,"违章图片01"));
        list2.add(new sptp(2,R.drawable.t2,"违章图片01"));
        list2.add(new sptp(3,R.drawable.t3,"违章图片01"));
        list2.add(new sptp(4,R.drawable.t4,"违章图片01"));
        getsplb();
        binding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(tab.getText().toString().equals("违章视频")){
                    getsplb();
                }else{
                    gettplb();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        return root;
    }
    public void gettplb() {
        binding.myrec.setAdater(2, list2.size(), R.layout.itemsptp, new Cac() {
            @Override
            public void bix(View view, int position) {
                ItemsptpBinding bind = ItemsptpBinding.bind(view);
                sptp sptp = list2.get(position);
                bind.img.setImageResource(sptp.tp);
                bind.text.setText(sptp.name);

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.e("TAG", "onClick: 点击列表");
                        MainActivity2spxq.start(getContext(),sptp.tp,sptp.name,1);
                    }
                });
            }
        });
    }
    public void getsplb() {
        binding.myrec.setAdater(2, list1.size(), R.layout.itemsptp, new Cac() {
            @Override
            public void bix(View view, int position) {
                ItemsptpBinding bind = ItemsptpBinding.bind(view);
                sptp sptp = list1.get(position);
                bind.img.setImageResource(sptp.tp);
                bind.text.setText(sptp.name);

                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.e("TAG", "onClick: 点击列表");
                        MainActivity2spxq.start(getContext(),sptp.sp,sptp.name,0);
                    }
                });
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}